package com.gamota.mobi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.androidquery.util.AQUtility;
import com.gamota.mobi.util.Constants;
import com.gamota.mobi.util.Utils;
import com.google.analytics.tracking.android.EasyTracker;

public class BaseActivity extends SherlockActivity {

	private int mTitleRes;
//	protected ListFragment mFrag;
    private String language="";
	public BaseActivity(int titleRes) {
		mTitleRes = titleRes;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		language= UILApplication.getInstance().getConfig().locale.getLanguage();
	}

	private void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.list_pager);
		setTitle(mTitleRes);
		EasyTracker.getInstance().setContext(this);
		// set the Behind View
//		setBehindContentView(R.layout.menu_frame);
//		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
//		mFrag = new MenuFragment();
//		t.replace(R.id.menu_frame, mFrag);
//		t.commit();
		
		// customize the SlidingMenu
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@SuppressLint("NewApi")
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if(language.equals(UILApplication.getInstance().getConfig().locale.getLanguage())){
			super.onResume();
		}else{
			super.onResume();
			initView();
			language= UILApplication.getInstance().getConfig().locale.getLanguage();
		}
		invalidateOptionsMenu();
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_home, menu);
//		this.menu = menu;
		return true;
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(isTaskRoot()){
            //clean the file cache with advance option
            long triggerSize = 10000000; //starts cleaning when cache size is larger than 10M
            long targetSize = 2000000;      //remove the least recently used files until cache size is less than 2M
            AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
		}
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		Log.e("", "prepare "+getResources().getString(R.string.account));
		
	menu.findItem(R.id.menu_account)
		.setTitle(getResources().getString(R.string.account));
	menu.findItem(R.id.menu_manage)
		.setTitle(getResources().getString(R.string.manage));
	menu.findItem(R.id.menu_setting)
		.setTitle(getResources().getString(R.string.setting));
		Log.e("", "prepared "+menu.findItem(R.id.menu_account).getTitle());
		
		return true;
	}
}
